class_name BaseDoor
extends StaticBody2D

#region 属性
@export var door_type:GameEnum.RoomDoorType
@export var door_area2d:Area2D
@export var door_open_condition:Condition
@export var door_open_cost:Cost
var active:bool = false
var door_state:GameEnum.RoomDoorState = GameEnum.RoomDoorState.NoActive
var cur_room:BaseRoom
var target_room:BaseRoom
var door_entity:Entity
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
#endregion
#region 公共方法
func init(cur_room:BaseRoom,target_room:BaseRoom,args:Dictionary = {}):
	door_entity = Utils.find_child_by_type(self,Entity)
	self.cur_room = cur_room
	self.target_room = target_room
	if self.target_room:
		set_active(true)
		hide_door()
	else:
		set_active(false)
func set_active(active:bool):
	self.active = active
	visible = self.active
func show_door():
	if door_state == GameEnum.RoomDoorState.Hide:
		door_state = GameEnum.RoomDoorState.Close
	visible = true
func hide_door():
	if door_state == GameEnum.RoomDoorState.NoActive:
		door_state = GameEnum.RoomDoorState.Hide
	visible = false
func open_door():
	if door_state == GameEnum.RoomDoorState.Close:
		var player = GamePlayerSystem.player
		if _check_open_condition(player) and _check_open_cost(player):
			door_state = GameEnum.RoomDoorState.Open
		var animation_component:AnimationComponent = door_entity.get_component(AnimationComponent)
		if animation_component:
			animation_component.play_animation("Open")
func close_door():
	if door_state == GameEnum.RoomDoorState.Open:
		door_state = GameEnum.RoomDoorState.Close
		var animation_component:AnimationComponent = door_entity.get_component(AnimationComponent)
		if animation_component:
			animation_component.play_animation("Close")
func translate_target_room():
	if door_state == GameEnum.RoomDoorState.Open:
		pass
func destroy():
	queue_free()
#endregion
#region 私有方法
func _check_open_condition(player:Player) -> bool:
	if door_open_condition:
		var entity:Entity = Utils.find_child_by_type(player,Entity,-1)
		return door_open_condition.check_result(entity)
	return true
func _check_open_cost(player:Player) -> bool:
	if door_open_cost:
		var entity:Entity = Utils.find_child_by_type(player,Entity,-1)
		return door_open_cost.check_cost(entity)
	return true
func _on_enter_door(body:Node2D):
	if door_state == GameEnum.RoomDoorState.Open:
		var player = body.get_parent()
		if player and player is Player:
			var entity:Entity = Utils.find_child_by_type(player,Entity,-1)
			if door_open_cost and door_open_cost.do_cost(entity):
				translate_target_room()
func _on_exit_door(body:Node2D):
	if door_state == GameEnum.RoomDoorState.Open:
		var player = body.get_parent()
		if player and player is Player:
			pass
#endregion
#region 生命周期函数
#endregion
